package com.cypher.encryptionanddecryption.dao.impl;

import com.cypher.encryptionanddecryption.bean.OtaBean;
import com.cypher.encryptionanddecryption.dao.OtaDao;
import com.cypher.encryptionanddecryption.util.DbUtils;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 通信信息Dao实现类
 * @author: 古霸天
 * @date: 2020/11/27
 */
public class OtaDaoImpl implements OtaDao {

    @Override
    public List<OtaBean> findAll() {
        String sql = "SELECT * FROM tb_ota ORDER BY ID ASC";
        return getBeans(sql, null);
    }

    private  List<OtaBean> getBeans(String sql, Map<Integer, Object> params) {
        List<OtaBean> beans = new ArrayList();
        PreparedStatement ps = DbUtils.getPreparedStatement(sql, params);
        if (ps == null) return beans;

        try {
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                beans.add(wrap(rs));
            }
            DbUtils.closeConn(ps, rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return beans;
    }

    public OtaBean wrap(ResultSet rs) throws SQLException {
        OtaBean bean = new OtaBean();
        bean.setId(rs.getInt("id"));
        bean.setName(rs.getString("filename"));
        bean.setData(rs.getString("filedata"));
        return bean;
    }

}
